import React, { memo, useMemo } from 'react';
import { Icon, Typography, Tooltip } from 'antd';

import Pagination from '@/components/Pagination';
import SummaryTable from '@/components/SummaryTable';

import styles from './index.less';

const SCROLL_CONFIG = { x: 'max-content' };

/**
 * @param rowKey
 * @param tableHeader
 * @param { ColumnProcessor } columnProcessor
 * @param dataSource
 * @param pagination
 * @param dispatch
 * @param dispatchType
 * @param hideSelection
 * @returns {*}
 * @constructor
 */
const CheckBody = ({
    rowKey,
    tableHeader,
    columnProcessor,
    summaryFields = [],
    dataSource,
    pagination,
    dispatch,
    dispatchType = {},
    changePage,
    tableLoading,
    hideSecurityTip = false,
    hideSelection,
    selectedRows = [],
    tableProps = {}
}) => {
    const { tablePageChangeType, selectRow } = dispatchType;

    const tablePageChange = ({ current, pageSize }) => {
        // eslint-disable-next-line no-unused-expressions
        tablePageChangeType
            ? dispatch({
                  type: tablePageChangeType,
                  payload: {
                      current,
                      pageSize
                  }
              })
            : changePage({ current, pageSize });
    };

    //  重新组装头部
    const renderColumns = useMemo(() => columnProcessor.merge(tableHeader).filter(item => !item.hidden), [
        columnProcessor,
        tableHeader
    ]);

    const vvSummaryFields = useMemo(() => {
        if (!summaryFields) {
            return [];
        }

        if (Array.isArray(summaryFields)) {
            return summaryFields;
        }

        return summaryFields(tableHeader);
    }, [summaryFields, tableHeader]);

    const dynamicProps = useMemo(
        () =>
            hideSelection
                ? {}
                : {
                      rowSelection: {
                          type: 'checkbox',
                          fixed: true,
                          columnWidth: 50,
                          selectedRowKeys: selectedRows.map(item => item.id),
                          onChange(selectedRowKeys, selectedRowsNew) {
                              dispatch({
                                  type: selectRow,
                                  payload: selectedRowsNew
                              });
                          },
                          getCheckboxProps: record => ({
                              disabled:
                                  record?.buttons?.filter(item => item.code === 9 || item.code === 15)[0]?.disabled ===
                                  true
                          })
                      }
                  },
        [hideSelection, selectedRows, dispatch]
    );

    return (
        <div className={styles.CheckBody}>
            <div>
                <SummaryTable
                    {...dynamicProps}
                    bordered
                    columns={renderColumns}
                    vvSummaryFields={vvSummaryFields}
                    rowKey={rowKey}
                    dataSource={dataSource}
                    pagination={false}
                    loading={tableLoading}
                    scroll={SCROLL_CONFIG}
                    {...tableProps}
                />
            </div>
            {hideSecurityTip ? null : (
                <div className={styles.securityTip}>
                    <Tooltip
                        overlay={
                            <ul>
                                <li>薪资细项所有的薪酬数据均采用非对称加密算法，即便数据库管理员也无法查看原始数据</li>
                                <li>薪酬管理员只可查看和管理自己发放的薪资条</li>
                            </ul>
                        }
                        placement="topLeft"
                        overlayClassName={styles.dropdownOverlay}
                    >
                        <span style={{ cursor: 'pointer' }}>
                            <Typography.Text type="secondary" style={{ marginRight: 5 }}>
                                数据隐私
                            </Typography.Text>
                            <Icon type="question-circle" />
                        </span>
                    </Tooltip>
                </div>
            )}

            <Pagination {...pagination} onChange={tablePageChange} />
        </div>
    );
};

export default memo(CheckBody);
